package org.bambrikii.kbManager.kb2.managers.query4;

public abstract class ConditionBase<T> implements ICondition<T> {
	private ConditionType type;
	private ICondition<T> parent;

	public ConditionBase(ConditionType type) {
		setType(type);
	}

	private class PropertyNameAliasPair {
		private String name;
		private String alias;

		public PropertyNameAliasPair(String name, String alias) {
			setName(name);
			setAlias(alias);
		}

		public String getName() {
			return name;
		}

		private void setName(String name) {
			this.name = name;
		}

		public String getAlias() {
			return alias;
		}

		private void setAlias(String alias) {
			this.alias = alias;
		}
	}

	protected PropertyNameAliasPair getTableAlias() {
		switch (getType()) {
		case SOURCE:
			return new PropertyNameAliasPair("source", "s1");
		case KIND:
			return new PropertyNameAliasPair("kind", "k1");
		default:
			return null;
		}
	}

	public ConditionSource getFrom() {
		ConditionSource cs = null;
		PropertyNameAliasPair subAlias = getTableAlias();
		if (parent != null) {
			ConditionSource cs1 = parent.getFrom();
			cs = new ConditionSource(cs1.getName() + "." + subAlias.getName(),
					cs1.getAlias() + "_" + subAlias.getAlias());
		} else {
			cs = new ConditionSource("src." + subAlias.getName(),
					subAlias.getAlias());
		}
		return cs;
	}

	public ConditionType getType() {
		return type;
	}

	protected void setType(ConditionType type) {
		this.type = type;
	}

	public void setParent(ICondition<T> parent) {
		this.parent = parent;
	}

	protected ICondition<T> getParent() {
		return parent;
	}
}
